package com.cheer.ba.presenter.impl;

import android.content.Context;

import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.Constants;
import com.cheer.ba.constant.ReturnCode;
import com.cheer.ba.http.BaseCallback;
import com.cheer.ba.http.CallManager;
import com.cheer.ba.http.HandleRetCode;
import com.cheer.ba.http.Http;
import com.cheer.ba.http.request.APIService;
import com.cheer.ba.model.CouponCardModel;
import com.cheer.ba.model.MyOrderModel;
import com.cheer.ba.model.ResponseData;
import com.cheer.ba.presenter.GeneralPresenter;
import com.cheer.ba.utils.NetUtils;
import com.cheer.ba.view.base.IViewController;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * 处理我的卡券
 *
 * @author HQ
 *         created at 2017/4/24 0024 10:54
 */
public class CardGroupPresenter extends GeneralPresenter {

    private Context mContext;
    private IViewController mView;
    private APIService mUserService;

    private int categroy;

    //记录当前分页下标
    private int mRecordPage = 0;
    private int mIndex = 0;

    //数据是否加载失败
    private boolean isDataError = false;

    public void setCategroy(int categroy) {
        this.categroy = categroy;
    }

    @Override
    public void attachView(IViewController view) {
        this.mView = view;
        mContext = getContext(view);
        mUserService = Http.getUserService(mContext);
    }

    @Override
    public void loadData(final boolean loadMore) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "", loadMore);
            return;
        }
        if (!loadMore) {
            isDataError = false;
            mRecordPage = 0;
        } else {
            if (isDataError) {
                mRecordPage = mIndex;
            } else {
                mRecordPage += Constants.PAGESIZE;
            }
        }
        mIndex = mRecordPage;
//        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "", loadMore);
        Call<ResponseData<List<CouponCardModel>>> call = mUserService.getCouponCardList(1508, Session.getUserId(), categroy, mIndex, Constants.PAGESIZE);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<CouponCardModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<CouponCardModel>>> call, Response<ResponseData<List<CouponCardModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<CouponCardModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<CouponCardModel> cards = res.getData();
                        if (null != cards) {
                            if (cards.isEmpty()) {
                                isDataError = true;
                            } else {
                                isDataError = false;
                            }
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "", loadMore);
                            mView.updateViewWithLoadMore(cards, loadMore, ConstantTag.MY_CARD_GROUP);
                            return;
                        }
//                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, "", loadMore);
//                        return;
                    }
                }
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }

            @Override
            public void onFailure(Call<ResponseData<List<CouponCardModel>>> call, Throwable t) {
                super.onFailure(call, t);
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }
        });
    }

    /**
     * 获得我的订单列表
     *
     * @param orderState -1全部1待支付4已支付5完成
     * @param loadMore
     */
    public void getMyOilCards(boolean isShowLoad, final boolean loadMore, String orderState) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "", loadMore);
            return;
        }
        if (!loadMore) {
            isDataError = false;
            mRecordPage = 0;
        } else {
            if (isDataError) {
                mRecordPage = mIndex;
            } else {
                mRecordPage += Constants.PAGESIZE;
            }
        }
        mIndex = mRecordPage;
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "", loadMore);
        }
        Call<ResponseData<List<MyOrderModel>>> call = mUserService.userOrders(1508, Session.getUserId(), categroy + "", mRecordPage, Constants.PAGESIZE);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<MyOrderModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<MyOrderModel>>> call, Response<ResponseData<List<MyOrderModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<MyOrderModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<MyOrderModel> cards = res.getData();
                        if (null != cards) {
                            isDataError = false;
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "", loadMore);
                            mView.updateViewWithLoadMore(cards, loadMore, ConstantTag.MY_OIL_CARDS);
                            return;
                        }
//                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, "", loadMore);
//                        return;
                    }
                }
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }

            @Override
            public void onFailure(Call<ResponseData<List<MyOrderModel>>> call, Throwable t) {
                super.onFailure(call, t);
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }
        });
    }

    /**
     * 获得用户可用的优惠券列表
     *
     * @param loadMore
     */
    public void getMyDiscountList(final boolean loadMore, String orderMoney) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "", loadMore);
            return;
        }
        if (!loadMore) {
            isDataError = false;
            mRecordPage = 0;
        } else {
            if (isDataError) {
                mRecordPage = mIndex;
            } else {
                mRecordPage += Constants.PAGESIZE;
            }
        }
        mIndex = mRecordPage;
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "", loadMore);
        Call<ResponseData<List<CouponCardModel>>> call = mUserService.getUserDiscountList(1508, Session.getUserId(), orderMoney, mIndex, Constants.PAGESIZE);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<CouponCardModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<CouponCardModel>>> call, Response<ResponseData<List<CouponCardModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<CouponCardModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<CouponCardModel> cards = res.getData();
                        if (null != cards) {
                            if (cards.isEmpty()) {
                                isDataError = true;
                            } else {
                                isDataError = false;
                            }
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "", loadMore);
                            mView.updateViewWithLoadMore(cards, loadMore, ConstantTag.MY_CARD_GROUP);
                            return;
                        }
//                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, "", loadMore);
//                        return;
                    }
                }
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }

            @Override
            public void onFailure(Call<ResponseData<List<CouponCardModel>>> call, Throwable t) {
                super.onFailure(call, t);
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }
        });
    }

    /**
     * 取消待支付的订单
     *
     * @param orderId 订单id
     */
    public void cancelPayOrder(String orderId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        Call<ResponseData> call = mUserService.cancelOrder(1508, orderId, Session.getUserId());
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag(res.getRet(), ConstantTag.CANCEL_ORDER_SUCCESS);
                        return;
                    } else {
                        HandleRetCode.handler(mContext, res, true, "取消失败");
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "取消失败");
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "取消失败");
            }
        });
    }

    /**
     * 已支付的订单申请退款
     *
     * @param orderId 订单id
     */
    public void askBackMoneyOrder(String orderId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        Call<ResponseData> call = mUserService.askBackMoneyOrder(1508, orderId, Session.getUserId());
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag(res.getRet(), ConstantTag.CANCEL_ORDER_SUCCESS);
                        return;
                    } else {
                        HandleRetCode.handler(mContext, res, true, "操作失败");
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "操作失败");
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "操作失败");
            }
        });
    }

    /**
     * 查看订单详情
     *
     * @param orderId 订单id
     */
    public void lookOrderDetails(String orderId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET);
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING);
        Call<ResponseData<MyOrderModel>> call = mUserService.lookOrderDetails(1508, orderId, Session.getUserId());
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<MyOrderModel>>() {
            @Override
            public void onResponse(Call<ResponseData<MyOrderModel>> call, Response<ResponseData<MyOrderModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<MyOrderModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        MyOrderModel model = res.getData();
                        if (null != model) {
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL);
                            mView.updateViewWithTag(model, ConstantTag.GET_ORDER_DETAILS);
                            return;
                        }
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
            }

            @Override
            public void onFailure(Call<ResponseData<MyOrderModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
            }
        });
    }

    @Override
    public void detachView() {

    }

    @Override
    public Object getCacheData() {
        return null;
    }
}
